package com.vates.reservas.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vates.reservas.dominio.Cliente;
import com.vates.reservas.service.ClienteService;

@Controller
@RequestMapping(value = "/cliente/alta")
public class ClienteAltaController {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private ClienteService clienteService;

	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(@Valid Cliente cliente, BindingResult result) {
		logger.info("BindingResult: " + result);
		if (result.hasErrors()) {
			return "cliente";
		}
		String nombre = cliente.getNombre();
		String nif = cliente.getNif();
		String domicilio = cliente.getDomicilio();
		String localidad = cliente.getLocalidad();
		String provincia = cliente.getProvincia();
		String codigoPostal = cliente.getCodigoPostal();

		logger.info("Registrando Cliente");

		clienteService.alta(nombre, nif, domicilio, localidad, provincia,
				codigoPostal);

		return "redirect:/cliente/listado";
	}

	@RequestMapping(method = RequestMethod.GET)
	protected Cliente formBackingObject(HttpServletRequest request)
			throws ServletException {
		return new Cliente();
	}

	public void setClienteService(ClienteService clienteService) {
		this.clienteService = clienteService;
	}
}